package com.share.factory;

import com.share.common.enums.AccountTypeEnum;
import com.share.common.enums.HttpStatusEnum;
import com.share.exception.BaseException;
import com.share.factory.strategy.AbstractAuthStategy;
import com.share.factory.strategy.AuthStrategy;
import com.share.factory.strategy.GiteAuthStrategy;
import com.share.factory.strategy.WeChatAuthStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.time.Instant;

// 认证策略工厂
@Service
public class AuthStrategyFactory implements ApplicationContextAware {
    //使用spring容器进行管理
   private ApplicationContext applicationContext;

    public AbstractAuthStategy getAuthStrategy(AccountTypeEnum authType) {
        return switch (authType) {
            case SYSTEM -> null;
            case WECHAT -> applicationContext.getBean(WeChatAuthStrategy.class);
            case GITE -> applicationContext.getBean(GiteAuthStrategy.class);
            default -> throw new BaseException(HttpStatusEnum.GONE,"无效的登录请求", Instant.now());
        };
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}